PowerSNMP for .NET
Create an Enum from MIBs



In addition to dynamically loading MIB files (as demonstrated in the Mib Treeview sample application), it is possible to compile MIB files into code files that can be directly added to any C#, VB or C++/CLI project. The benefit of this technique is the Intellisense it provides for the imported MIB objects, tables, etc.

Creating Code Files

This feature can be used in two ways.  First, the MibNodes.GenerateCode function can be called after loading your MIB files. This is demonstrated in the Mib Treeview sample application. Example code might look like the following:

C#
Copy Code
private void generateCodeFile_CS(MibNodes nodes, string path)
{
    using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
        nodes.GenerateCode(file, Language.CSharp);
}

Visual Basic
Copy Code
Private Sub generateCodeFile_VB(ByVal nodes As MibNodes, ByVal path As String)
    Using file As New FileStream(path, FileMode.Create, FileAccess.Write)
        nodes.GenerateCode(file, Language.VisualBasic)
    End Using
End Sub

It is also possible to generate code files in the Visual Studio designer by right-clicking on the Manager or Agent component and selecting "Import MIB Files."  This will invoke a dialog where MIB files can be selected and compiled into code files in the language of your choice. The generated code file can then be automatically added to the project.

Importing Code Files

Once the code file has been added to a project, the developer can import the new objects into a MibNodes collection by calling Mib.Import(MibNodes).

C#
Copy Code
private void importMibCodeFile()
{
    Mib.Import(manager1.Mib);
}
Visual Basic
Copy Code
Private Sub importMibCodeFile()
    Mib.Import(manager1.Mib)
End Sub

The new objects will now appear to the developer through Intellisense, just as the "built-in" objects are.

C#
Copy Code
MibNode integratedNode = manager1.Mib.GetByNodeName(NodeName.sysContact);
MibNode newNode = manager1.Mib.GetByNodeName(Mib.NodeName.myImportedObject);

Visual Basic
Copy Code
Dim integratedNode As New MibNode = manager1.Mib.GetByNodeName(NodeName.sysContact)
Dim newNode As New MibNode = manager1.Mib.GetByNodeName(Mib.NodeName.myImportedObject)

Note that imported objects are accessed using the Mib.NodeName enumeration (not Dart.Snmp.NodeName, like the integrated objects), as below.

C#
Copy Code
/*  This snippet uses a Mib code file generated from the following MIB file content
    (our included MIB Treeview sample may be used to easily create a Mib code file):
        
DART-TEST-MIB DEFINITIONS ::= BEGIN

IMPORTS
        private
                FROM RFC1155-SMI
        OBJECT-TYPE
                FROM RFC-1212
        DisplayString
                FROM RFC-1213;

dart        OBJECT IDENTIFIER ::= {private 42}

testString OBJECT-TYPE
    SYNTAX  DisplayString
    ACCESS  read-write
    STATUS  optional
    DESCRIPTION
            "This is a description for a string"
    ::= {dart 1}

*/

private void Form1_Load(object sender, EventArgs e)
{
    //Import the Mib code file (must included in the project) generated from the MIB defined above
    Mib.Import(manager1.Mib);
}

private void button1_Click(object sender, EventArgs e)
{
    //Create and send request for 'testString' from the Mib code file, on a worker thread
    manager1.Start(manager1_SendGetRequest, manager1.Mib.CreateVariable(Mib.NodeName.testString));
}

private void manager1_SendGetRequest(SnmpSocket managerSocket, object state)
{
    //Create Get Request
    GetMessage request = new GetMessage();
    request.Variables.Add(state as Variable);

    //Send request and get response
    ResponseMessage response = managerSocket.GetResponse(request, myAgentAddress);

    //Marshal message to the UI thread using the Message event
    manager1.Marshal(new ResponseMessage[] { response }, "", null);
}

private void manager1_Message(object sender, MessageEventArgs e)
{
    //Display info about the first variable in the response, and its value
    Variable vari = e.Messages[0].Variables[0];
    label1.Text = vari.Definition.ToString() + vari.Value.ToString();
}
Visual Basic
Copy Code
'  This snippet uses a Mib code file generated from the following MIB file content
'    (our included MIB Treeview sample may be used to easily create a Mib code file):
'        
'DART-TEST-MIB DEFINITIONS ::= BEGIN
'
'IMPORTS
'        private
'                FROM RFC1155-SMI
'        OBJECT-TYPE
'                FROM RFC-1212
'        DisplayString
'                FROM RFC-1213;
'
'dart        OBJECT IDENTIFIER ::= {private 42}
'
'testString OBJECT-TYPE
'    SYNTAX  DisplayString
'    ACCESS  read-write
'    STATUS  optional
'    DESCRIPTION
'            "This is a description for a string"
'    ::= {dart 1}
'
'

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    'Import the Mib code file (must included in the project) generated from the MIB defined above
    Mib.Import(manager1.Mib)
End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create and send request for 'testString' from the Mib code file, on a worker thread
    manager1.Start(AddressOf manager1_SendGetRequest, manager1.Mib.CreateVariable(Mib.NodeName.testString))
End Sub

Private Sub manager1_SendGetRequest(ByVal managerSocket As SnmpSocket, ByVal state As Object)
    'Create Get Request
    Dim request As New GetMessage()
    request.Variables.Add(TryCast(state, Variable))

    'Send request and get response
    Dim response As ResponseMessage = managerSocket.GetResponse(request, myAgentAddress)

    'Marshal message to the UI thread using the Message event
    manager1.Marshal(New ResponseMessage() { response }, "", Nothing)
End Sub

Private Sub manager1_Message(ByVal sender As Object, ByVal e As MessageEventArgs)
    'Display info about the first variable in the response, and its value
    Dim vari As Variable = e.Messages(0).Variables(0)
    label1.Text = vari.Definition.ToString() & vari.Value.ToString()
End Sub
See Also

PowerSNMP for .NET Documentation Version 8.0
© 2024 Dart Communications. All Rights Reserved.
Send comments on this topic